//
//  HTAlertFooterView.m
//  TruckUser
//
//  Created by Ting Han on 2017/11/2.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTAlertFooterView.h"
#import "HTButtonCell.h"

@interface HTAlertFooterView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UITextViewDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textViewHeight;

@end

@implementation HTAlertFooterView
- (void)setDataSource:(NSArray *)dataSource{
    _dataSource = dataSource;
    [self.collectionView reloadData];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"HTButtonCell" bundle:nil] forCellWithReuseIdentifier:ButtonCell];
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.flowLayout.estimatedItemSize = CGSizeMake(80, 35);
    self.flowLayout.minimumLineSpacing = 10;
    self.flowLayout.minimumInteritemSpacing = 10;
    self.textView.placeholder = @"限50字";
    self.textViewHeight.constant = 45;
    self.textView.delegate = self;
    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HTButtonCell *button = [collectionView dequeueReusableCellWithReuseIdentifier:ButtonCell forIndexPath:indexPath];
    button.mainButton.tag = indexPath.row;
    button.titleStr =self.dataSource[indexPath.row];
    [button.mainButton addTarget:self action:@selector(didClickItem:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}
- (void)didClickItem:(UIButton *)sender{
    if (self.textView.hasText) {
        if (self.textView.text.length +[self.dataSource[sender.tag] length] >50 ) {
            [HTShowUtil showMessage:@"最多输入50字"];
            return;
        }
        self.textView.text = [NSString stringWithFormat:@"%@,%@",self.textView.text,self.dataSource[sender.tag]];
    }else{
        self.textView.text = [NSString stringWithFormat:@"%@%@",self.textView.text,self.dataSource[sender.tag]];
    }
}

#pragma mark --- 回调
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    if (textView == self.textView) {
        if (textView.text.length + text.length > 50 && text.length > 0) {
            [HTShowUtil showMessage:@"最多输入50字"];
            return NO;
        }else{
            return YES;
        }
    }
    return YES;
}


@end
